rm(list=ls())

# load packages

packages <- c("foreign", "maptools", "ggmap", "RColorBrewer", "cowplot", "ggpubr", "dplyr")

package_installed <-
  sapply(packages, function(pack)
    pack %in% rownames(installed.packages()))

if (any(!package_installed)) {
  sapply(packages[!package_installed], install.packages)
}

sapply(packages, require, character.only = TRUE)
rm(packages,package_installed)

# read data & shapefiles

county <- read.dta("tradewar_io_county.dta")

county$repshare_break <- cut(county$county_repshare_house1416, breaks = c(-1, 10*(2:8), 100))
county$emp_chn_target_april_break <- cut(county$emp_chn_target_april, breaks = c(-1, 2.5*(0:8),100))
county$emp_chn_target_cum_june_break <- cut(county$emp_chn_target_cum_june, breaks = c(-1, 2.5*(0:8),100))
county$emp_chn_target_cum_aug_break <- cut(county$emp_chn_target_cum_aug, breaks = c(-1, 2.5*(0:8),100))
county$emp_chn_target_cum_sept_break <- cut(county$emp_chn_target_cum_sept, breaks = c(-1, 2.5*(0:8),100))
county$emp_us_protect_cum_june_break <- cut(county$emp_us_protect_cum_june, breaks = c(-1, 2.5*(0:8),100))
county$emp_us_protect_cum_aug_break <- cut(county$emp_us_protect_cum_aug, breaks = c(-1, 2.5*(0:8),100))
county$emp_us_protect_cum_sept_break <- cut(county$emp_us_protect_cum_sept, breaks = c(-1, 2.5*(0:8),100))

county_shape <- readShapePoly("cb_2017_us_county_500k.shp")
county_shape.df <- fortify(county_shape)
county_shape.df <- data.frame(county_shape.df, county_shape@data[county_shape.df$id, ])
county_shape.df <- county_shape.df[county_shape.df$long < -50 & county_shape.df$long > -135 & county_shape.df$lat < 50 & county_shape.df$lat > 22, ]
names(county_shape.df)[12] <- "fipscode"

county_shape.df <- merge(county_shape.df, county, by = intersect(names(county_shape.df), names(county)))

# Figure 1: The Map of County-Level Exposure to Chinese Retaliatory Tariffs

mycolors = c("white", brewer.pal(n = 9, name = "Reds"))

pdf("Figure1-April.pdf",width=7,height=4)
ggplot(data = county_shape.df[!is.na(county_shape.df$emp_chn_target_april_break),], mapping = aes(x = long, y = lat, fill = emp_chn_target_april_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="none") + coord_map(project="conic", lat0 = 30)
dev.off()

pdf("Figure1-June.pdf",width=7,height=4)
ggplot(data = county_shape.df[!is.na(county_shape.df$emp_chn_target_cum_june_break),], mapping = aes(x = long, y = lat, fill = emp_chn_target_cum_june_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="none") +  coord_map(project="conic", lat0 = 30)
dev.off()

pdf("Figure1-August.pdf",width=7,height=4)
ggplot(data = county_shape.df[!is.na(county_shape.df$emp_chn_target_cum_aug_break),], mapping = aes(x = long, y = lat, fill = emp_chn_target_cum_aug_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="none") + coord_map(project="conic", lat0 = 30)
dev.off()

pdf("Figure1-September.pdf",width=7,height=4)
ggplot(data = county_shape.df[!is.na(county_shape.df$emp_chn_target_cum_sept_break),], mapping = aes(x = long, y = lat, fill = emp_chn_target_cum_sept_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="none") + coord_map(project="conic", lat0 = 30)
dev.off()

legend_plot <- ggplot(data = county_shape.df[!is.na(county_shape.df$emp_chn_target_cum_sept_break),], mapping = aes(x = long, y = lat, fill = emp_chn_target_cum_sept_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.text = element_text(size = 7)) + coord_map(project="conic", lat0 = 30)
pdf("Figure1-legend.pdf",width=1,height=4)
legend <- get_legend(legend_plot)
as_ggplot(legend)
dev.off()

# Figure 2: The Republican Share of the Two-Party Vote for House of Representatives and the Competitive Districts in the 2014 and 2016 Elections 

mycolors2 = c("#053061", "#2166AC", "#4393C3", "#D1E5F0", "#FDDBC7", "#D6604D", "#B2182B", "#67001F")

pdf("Figure2.pdf",width=7,height=4)
mapPlot <- ggplot() + theme_bw() + theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.border = element_blank(),axis.title = element_blank(),axis.text = element_blank(),axis.ticks = element_blank(),legend.position="bottom",legend.key.width = unit(2, "cm")) + geom_polygon(data = county_shape.df[!is.na(county_shape.df$repshare_break),], aes(x = long,y = lat,group = group, fill = county_shape.df[!is.na(county_shape.df$repshare_break),]$repshare_break),colour = "gray85",lwd = 1/50) + scale_fill_manual("", values = mycolors2,labels = c("<20%", "20-30%", "30-40%", "40-50%", "50-60%", "60-70%", "70-80%", ">80%")) + theme(legend.position="bottom", legend.key.size = unit(0.3, "cm"), legend.key.width=unit(0.4, "cm")) + guides(fill = guide_legend(nrow = 1)) 

swing_dist <- read.dta("tradewar_io_swing.dta")
names(swing_dist)[1] <- "STATEFP"
names(swing_dist)[2] <- "CD115FP"

dist_shape <- readShapePoly("cb_2017_us_cd115_500k.shp")
dist_shape.df <- fortify(dist_shape)
dist_shape.df <- data.frame(dist_shape.df, dist_shape@data[dist_shape.df$id, ])
dist_shape.df <- dist_shape.df[dist_shape.df$long < -50 & dist_shape.df$long > -135 & dist_shape.df$lat < 50 & dist_shape.df$lat > 22, ]
dist_shape.df <- left_join(dist_shape.df, swing_dist, by = intersect(names(dist_shape.df), names(swing_dist)))

mapPlot <- mapPlot + geom_polygon(data = dist_shape.df, aes(x = long,y = lat,group = group),fill = NA,colour = "gray47",lwd = 1/7)
mapPlot <- mapPlot + geom_polygon(data = subset(dist_shape.df, swing_dist1416 == 1), aes(x = long,y = lat,group = group),fill = NA,colour = "black",lwd = 1/2) + coord_map(project="conic", lat0 = 30)
plot(mapPlot)
dev.off()

# Figure A1: County-Level Employment Share Protected by the US-Initiated Tariffs

pdf("FigureA1-June.pdf",width=7,height=4)
ggplot(data = county_shape.df[!is.na(county_shape.df$emp_us_protect_cum_june_break),], mapping = aes(x = long, y = lat, fill = emp_us_protect_cum_june_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="none") +  coord_map(project="conic", lat0 = 30)
dev.off()

pdf("FigureA1-August.pdf",width=7,height=4)
ggplot(data = county_shape.df[!is.na(county_shape.df$emp_us_protect_cum_aug_break),], mapping = aes(x = long, y = lat, fill = emp_us_protect_cum_aug_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="none") + coord_map(project="conic", lat0 = 30)
dev.off()

pdf("FigureA1-Sept.pdf",width=7,height=4)
ggplot(data = county_shape.df[!is.na(county_shape.df$emp_us_protect_cum_sept_break),], mapping = aes(x = long, y = lat, fill = emp_us_protect_cum_sept_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="none") + coord_map(project="conic", lat0 = 30)
dev.off()

legend_plot <- ggplot(data = county_shape.df[!is.na(county_shape.df$emp_us_protect_cum_sept_break),], mapping = aes(x = long, y = lat, fill = emp_us_protect_cum_sept_break, group = group)) + geom_polygon(color = "gray90", size = 0.05) + coord_equal() + scale_fill_manual("", values = mycolors, labels = c("0%", "0-2.5%", "2.5-5%", "5-7.5%", "7.5-10%", "10-12.5%", "12.5-15%", "15-17.5%", "17.5-20%", "20%-" )) + theme_bw() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.text = element_text(size = 7)) + coord_map(project="conic", lat0 = 30)
pdf("FigureA1-legend.pdf",width=1,height=4)

legend <- get_legend(legend_plot)
as_ggplot(legend)
dev.off()
